package coolq.yaozhenyong.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.forte.qqrobot.BaseConfiguration;
import com.forte.qqrobot.anno.timetask.CronTask;
import com.forte.qqrobot.beans.messages.result.GroupInfo;
import com.forte.qqrobot.beans.messages.result.GroupList;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.timetask.TimeJob;
import com.forte.qqrobot.utils.CQCodeUtil;

import coolq.yaozhenyong.Impl.ApiCocportImpl;
import coolq.yaozhenyong.bean.ApiCocport;
import coolq.yaozhenyong.bean.Apiport;
import coolq.yaozhenyong.bean.SysUser;
import coolq.yaozhenyong.custom.SpringContextUtil;
import coolq.yaozhenyong.custom.TimeUtils;
import coolq.yaozhenyong.service.ApiCocportService;
import coolq.yaozhenyong.service.ApiportService;
import coolq.yaozhenyong.service.GroupListService;
import coolq.yaozhenyong.service.SysUserService;

@CronTask("0 0 0 * * ?")
public class TimingTwo implements TimeJob{
	
	//"0 0 0 * * ?"
	@Autowired
	@Override
	public void execute(MsgSender msgSender, CQCodeUtil cqCodeUtil) {
		try {
			/*FriendList friendList = msgSender.GETTER.getFriendList();
			Friend[] firendList = friendList.getFirendList("我的好友");
			for (int i = 0; i < firendList.length; i++) {
				msgSender.SENDER.sendPrivateMsg(firendList[i].getQQ(), "机器人提醒你今天已经是"+TimeUtils.getStringTime()+"凌晨了哦！早点休息哦！");
			}*/
			//正对合作用户的
			System.out.println(TimeUtils.getStringDate() + " 12点定时任务执行");
			
			try {
				administroator(msgSender, cqCodeUtil);
			} catch (Exception e) {
				System.out.println("处理合作用户失败！");
			}
			//重置部落冲突使用次数
			ApiCocportImpl apicoc = SpringContextUtil.getBean(ApiCocportImpl.class);
			List<ApiCocport> list = apicoc.list();
			for (ApiCocport apiCocport : list) {
				apiCocport.setResidue(apiCocport.getSum());
				apicoc.updateById(apiCocport);
			}
			msgSender.SENDER.sendPrivateMsg("936642284", "已有"+list.size()+"个COC接口被重置");
			/*msgSender.SETTER.setSign();*/
			/** 更新所有QQ群信息 **/
			//updateByGroupList(msgSender);
		} catch (Exception e) {
			System.out.println("12点通报出现bug");
		}
		
	}
	
	private void administroator(MsgSender msgSender, CQCodeUtil cqCodeUtil) {
		SysUserService user = SpringContextUtil.getBean(SysUserService.class);
		List<SysUser> list = user.query().eq("administratoring", "1").list();
			for (SysUser sysUser : list) {
				long datadifference = TimeUtils.timedifference(sysUser.getDate());
				long da = (long)60*60*24 - 1;
				if(sysUser.getDate()!=null&&sysUser.getDate()!="") {
					if(datadifference<=0) {
						//合作用户已取消
						sysUser.setAdministratoring(false);
						sysUser.setDate(null);
						user.updateById(sysUser);
						/** 修改查询部落冲突次数 **/
						ApiportService api = SpringContextUtil.getBean(ApiportService.class);
						Apiport Apiport = api.query().eq("qqnumber", sysUser.getUsername()).eq("type", "coc").one();
						if(Apiport!=null) {
							ApiCocportService bean = SpringContextUtil.getBean(ApiCocportService.class);
							ApiCocport one = bean.query().eq("uuid", Apiport.getAppkey()).one();
							if(one!=null) {
								one.setSum(5);
								one.setResidue(5);
								bean.updateById(one);
							}
						}
						msgSender.SENDER.sendPrivateMsg(sysUser.getQq(), "您好，合作关系已取消！合作服务终止于"+TimeUtils.getStringTime()+""
								+ "\n合作的资金均放在酷Q Pro插件服务和服务器上，仅为稳定大部分功能使用，不合作的功能只限制了"
								+ "发送图片，发送分享链接，发送音乐分享，违禁词消息撤回功能，发送语言的功能，限制了每日调用接口查询部落冲突信息的功能！如需继续合作，联系作者！");
					}
				}else if(datadifference>0&&datadifference<=da){
					msgSender.SENDER.sendPrivateMsg(sysUser.getQq(), "你好，合作将于"+sysUser.getDate()+"结束服务！届时将限制发送图片，发送分享链接，发送音乐分享，违禁词消息撤回功能，发送语言的功能，限制了每日调用接口查询部落冲突信息的功能！"
							+ "\n\n将在明日24时进行取消，如不需使用如上服务，您可无视次消息！");
				}
			}
	}

	/** 更新所有QQ群信息 暂时废弃 **/
	public void updateByGroupList(MsgSender msgSender) {
		GroupListService bean = SpringContextUtil.getBean(GroupListService.class);
		List<coolq.yaozhenyong.bean.GroupList> list = bean.list();
		for (coolq.yaozhenyong.bean.GroupList groupList : list) {
			try {
				GroupInfo g = msgSender.GETTER.getGroupInfo(groupList.getGroupcode());
				if(g!=null&&g.getName()!=null) {
					groupList.setName(g.getName());
					groupList.setLevel(g.getLevel()==null?"":g.getLevel()+"");
					groupList.setOpentype(g.getOpenType()==null?"":g.getOpenType()+"");
					String[] adminList = g.getAdminList();
					if(adminList!=null) {
						String admini = "";
						Boolean isgoto = false;
						Map<String, String> adminNickList = g.getAdminNickList();
						for (int i = 0; i < adminList.length; i++) {
							String string = adminList[i];
							String string2 = adminNickList.get(string);
							admini += string + "["+string2+"]"+",";
							if(string .equals(BaseConfiguration.getLocalQQCode())) {
								isgoto = true;
							}
						}
						groupList.setAdminlist(admini);
						if(isgoto) {
							groupList.setIsadmin("是管理");
						}else {
							groupList.setIsadmin(null);
						}
					}
					groupList.setBoard(g.getBoard());
					groupList.setComoleteintro(g.getCompleteIntro());
					groupList.setMaxnumber(g.getMaxMember()==null?"":g.getMaxMember()+"");
					groupList.setNumber(g.getMemberNum()==null?"":g.getMemberNum()+"");
					groupList.setOwnerqq(g.getOwnerQQ());
					groupList.setPos(g.getPos());
					bean.updateById(groupList);
				}else {
					System.out.println(groupList.getGroupcode()+"群信息无法获取信息！");
				}
			} catch (Exception e) {
				System.out.println(groupList.getGroupcode()+"群信息更新失败！");
			}
		}
	}

}
